﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace YFrameWork.RunTime
{
    public class MonoExtension : MonoSingleton<MonoExtension>
    {
        public  void RefreshLayout(RectTransform Rect)
        {
            StartCoroutine(IRefreshLayout(Rect));
        }
        public  void SetMiddleAnchor(RectTransform left, RectTransform right, float midInterval = 20)
        {
            StartCoroutine(DelaySetAnchor(left, right, midInterval));
        }

         IEnumerator IRefreshLayout(RectTransform Rect)
        {
            for (int i = 0; i < 3; i++)
            {
                yield return new WaitForEndOfFrame();
                foreach (var item in Rect.GetComponentsInChildren<RectTransform>())
                {
                    LayoutRebuilder.ForceRebuildLayoutImmediate(item);
                }
            }
        }


         IEnumerator DelaySetAnchor(RectTransform left, RectTransform right, float midInterval)
        {
            yield return null;
            yield return null;
            float leftSizeX = left.sizeDelta.x;
            float rightSizeX = right.sizeDelta.x;
            float totalSizeX = leftSizeX + rightSizeX;
            left.localPosition = new Vector3(-totalSizeX / 2 + leftSizeX / 2 - midInterval / 2, left.localPosition.y);
            right.localPosition = new Vector3(totalSizeX / 2 - rightSizeX / 2 + midInterval / 2, right.localPosition.y);
        }

    }
}